package com.pro.addresslist.ui.activity;

import java.util.ArrayList;
import java.util.List;

import com.pro.addresslist.R;
import com.pro.addresslist.dal.bll.ContactInfoBLL;
import com.pro.addresslist.dal.bll.IPMACBLL;
import com.pro.addresslist.dal.model.ContactInfo;
import com.pro.addresslist.dal.model.ContactInfoBySqlite;
import com.pro.addresslist.dal.model.IPMAC;
import com.pro.addresslist.sqllite.ContactInfoServise;
import com.pro.addresslist.sqllite.DBOpenHelper;
import com.pro.addresslist.tools.BASE64Encrypt;
import com.pro.addresslist.ui.adapter.CommonAdapter;
import com.pro.addresslist.ui.adapter.ViewHolder;
import com.pro.addresslist.ui.common.BaseActivity;
import com.pro.addresslist.ui.view.CircleImageView;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;
/**
 * 新增加本地数据库，数据查询。20161215.
 * @author Beyond
 *
 */
public class BirthdayReminder extends BaseActivity {
	private List<ContactInfoBySqlite> list = new ArrayList<ContactInfoBySqlite>();
	ArrayList<String> ChoosePhoneList = new ArrayList<String>();
	private GridView gr;
	private CommonAdapter<ContactInfoBySqlite> birthdayadapter;

	@Override
	public int getLayout() {
		// TODO Auto-generated method stub
		return R.layout.birthday_layout;
	}

/*	public class ReceiveBroadCast extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			String action = intent.getAction();
			if (action.equals("lbs_SelectContactBySR")) {
				// 得到广播中得到的数据，并显示出来
				List<ContactInfo> parameter = (List<ContactInfo>) intent
						.getSerializableExtra("parameter");
				for (ContactInfoBySqlite con : parameter) {
					list.add(con);
					ChoosePhoneList.add(con.get_MobileEn());
				}
				birthdayadapter.notifyDataSetChanged();
			}

		}
	}*/

	@Override
	public void OnActCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		setTtile("一声问候");
		setLeftImageBack();

		gr = (GridView) findViewById(R.id.birthday_grid);
		birthdayadapter = new CommonAdapter<ContactInfoBySqlite>(BirthdayReminder.this,list, R.layout.birthday_itmes) {

			@Override
			public void convert(ViewHolder holder, ContactInfoBySqlite t) {
				holder.setText(R.id.birth_img, t.get_Name().substring(0, 1));
				holder.setText(R.id.birth_name, t.get_Name());
				String[] date = t.get_Birthday().split("-");
				holder.setText(R.id.birth_date, date[1] + "月" + date[2] + "日");
			}
		};
		gr.setAdapter(birthdayadapter);
		Button btnQunfa = (Button) findViewById(R.id.btnQunfa);
		btnQunfa.setOnClickListener(this);

		//这里开始注册广播发送请求，现在用本地注释
	/*	ReceiveBroadCast receiveBroadCast = new ReceiveBroadCast();
		IntentFilter filter = new IntentFilter();
		filter.addAction("lbs_SelectContactBySR");
		registerReceiver(receiveBroadCast, filter);	
		ContactInfoBLL.SelectContactBySR(mContext);*/
		
		ContactInfoServise ser = new ContactInfoServise(getApplicationContext());
		List<ContactInfoBySqlite> parameter = new ArrayList<ContactInfoBySqlite>();
		parameter = ser.SelectContactBySR();
		ChoosePhoneList.clear();
		for (ContactInfoBySqlite con : parameter) {
			list.add(con);
			ChoosePhoneList.add(con.get_MobileEn());
		}
		birthdayadapter.notifyDataSetChanged();		
		
		
		// gride view item select state change
		gr.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				com.pro.addresslist.ui.view.CircleImageView cir = (com.pro.addresslist.ui.view.CircleImageView) view
						.findViewById(R.id.birth_top_right_img);
				CheckBox ck = (CheckBox) view.findViewById(R.id.ckbo);
				String nowPhone = list.get(position).get_MobileEn();
				Log.i("bt", "nowPhone-->"+nowPhone);
				if (ck.isChecked()) {
					cir.setImageResource(R.drawable.icon_noselect);
					ck.setChecked(false);	
					for(String i:ChoosePhoneList){
						if(i.equals(nowPhone)){
							ChoosePhoneList.remove(i);
							return;
						}
					}
				} else {
					cir.setImageResource(R.drawable.icon_select);
					ck.setChecked(true);
					ChoosePhoneList.add(nowPhone);
				}
			}
		});
	}

	@Override
	public void OnViewClick(View v) {
		switch (v.getId()) {
		case R.id.btnQunfa:
			if(ChoosePhoneList.size()<=0){
				Toast.makeText(getApplicationContext(), "亲！ 最近一个周没有人联系人可送上生日祝福！",Toast.LENGTH_SHORT).show();
				return;
			}
			ArrayList<String> remrepph = new ArrayList<String>();
			for(String i:ChoosePhoneList){
				String[] dm=i.split(",");
				remrepph.add(dm[0]);
				Log.i("bt", "remrepph-->"+i);
			}
			
			Intent inten = new Intent();
			inten.setClass(getApplicationContext(), SendMsgActivity.class);
			inten.putStringArrayListExtra("telNum", remrepph);
			startActivity(inten);
			
			
			// 获得所有选择的联系人
		/*	String flist ="";	
			if(ChoosePhoneList.size()<=0){
				Toast.makeText(getApplicationContext(), "亲！ 最近一个周没有人联系人可送上生日祝福！",Toast.LENGTH_SHORT).show();
				return;
			}
			for(String i:ChoosePhoneList){
			String[] dm=i.split(",");
			flist += dm[0]+",";
			Log.i("bt", "nowPhone-->"+i);
			}
			flist = flist.substring(0,flist.length()-1);
			Uri smsToUri = Uri.parse("smsto:"+flist);
			Intent intent = new Intent(Intent.ACTION_SENDTO, smsToUri);
			//intent.putExtra("sms_body", "测试发送短信");
			startActivity(intent);
			Log.i("bt", "sendnumber"+flist);	*/
			
			break;
		default:
			break;
		}
	}

}
